package com.ryder.petmatediarybackend.module.file.enums;

import lombok.Getter;

import java.util.Arrays;

/**
 * 描述: 文件上传业务类型枚举
 *
 * <p>说明：
 * 该枚举定义了宠伴日记系统中的文件上传业务场景类型。
 * 每种业务类型对应不同的存储路径、文件大小限制、格式限制和数量限制。
 * 枚举设计遵循阿里巴巴开发规范，便于后续业务扩展与文件管理。</p>
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025/10/30
 */
@Getter
public enum FileBizTypeEnum {

    USER_AVATAR("USER_AVATAR", "用户头像", "/avatar/user", 2 * 1024 * 1024L, 1, "jpg,jpeg,png", "file:user:avatar:upload"),
    PET_AVATAR("PET_AVATAR", "宠物头像", "/avatar/pet", 2 * 1024 * 1024L, 1, "jpg,jpeg,png", "file:pet:avatar:upload"),
    PET_ALBUM("PET_ALBUM", "宠物相册", "/album", 5 * 1024 * 1024L, 9, "jpg,jpeg,png", "file:pet:album:upload"),
    DIARY_IMAGE("DIARY_IMAGE", "日记图片", "/diary", 5 * 1024 * 1024L, 9, "jpg,jpeg,png", "file:diary:image:upload"),
    MOMENT_IMAGE("MOMENT_IMAGE", "动态图片", "/moment", 5 * 1024 * 1024L, 9, "jpg,jpeg,png", "file:moment:image:upload"),
    QUALIFICATION("QUALIFICATION", "资质证明", "/qualification", 10 * 1024 * 1024L, 10, "jpg,jpeg,png,pdf", "file:shop:qualification:upload"),
    SHOP_PET_COLLECTION("SHOP_PET_COLLECTION", "商家宠物合集", "/shop/pet", 5 * 1024 * 1024L, 30, "jpg,jpeg,png", "file:shop:pet:upload"),
    SHOP_PRODUCT_COLLECTION("SHOP_PRODUCT_COLLECTION", "商家商品合集", "/shop/product", 5 * 1024 * 1024L, 50, "jpg,jpeg,png", "file:shop:product:upload");

    /**
     * 业务类型编码（系统内部标识）
     */
    private final String code;

    /**
     * 中文名称（前端展示用）
     */
    private final String name;

    /**
     * 存储路径前缀
     */
    private final String pathPrefix;

    /**
     * 单个文件最大大小（字节）
     */
    private final Long maxFileSize;

    /**
     * 最大上传数量
     */
    private final Integer maxCount;

    /**
     * 允许的文件格式（逗号分隔）
     */
    private final String allowedFormats;

    /**
     * 所需权限
     */
    private final String requiredPermission;

    FileBizTypeEnum(String code, String name, String pathPrefix, Long maxFileSize, Integer maxCount, String allowedFormats, String requiredPermission) {
        this.code = code;
        this.name = name;
        this.pathPrefix = pathPrefix;
        this.maxFileSize = maxFileSize;
        this.maxCount = maxCount;
        this.allowedFormats = allowedFormats;
        this.requiredPermission = requiredPermission;
    }

    /**
     * 根据业务类型编码获取枚举
     *
     * @param code 业务类型编码
     * @return 对应的枚举，如果不存在返回null
     */
    public static FileBizTypeEnum fromCode(String code) {
        if (code == null) {
            return null;
        }
        return Arrays.stream(values())
                .filter(e -> e.code.equalsIgnoreCase(code))
                .findFirst()
                .orElse(null);
    }

    /**
     * 判断是否为头像类型
     *
     * @return true-是头像类型，false-不是
     */
    public boolean isAvatar() {
        return this == USER_AVATAR || this == PET_AVATAR;
    }

    /**
     * 判断是否支持批量上传
     *
     * @return true-支持批量，false-仅单张
     */
    public boolean supportBatch() {
        return this.maxCount > 1;
    }

    /**
     * 判断文件格式是否允许
     *
     * @param fileExtension 文件扩展名（不含点号）
     * @return true-允许，false-不允许
     */
    public boolean isFormatAllowed(String fileExtension) {
        if (fileExtension == null) {
            return false;
        }
        return this.allowedFormats.toLowerCase().contains(fileExtension.toLowerCase());
    }

    /**
     * 判断文件大小是否超限
     *
     * @param fileSize 文件大小（字节）
     * @return true-超限，false-未超限
     */
    public boolean isFileSizeExceeded(Long fileSize) {
        if (fileSize == null) {
            return true;
        }
        return fileSize > this.maxFileSize;
    }

    /**
     * 判断上传数量是否超限
     *
     * @param count 上传数量
     * @return true-超限，false-未超限
     */
    public boolean isCountExceeded(Integer count) {
        if (count == null) {
            return true;
        }
        return count > this.maxCount;
    }

    /**
     * 获取文件大小限制描述（MB）
     *
     * @return 文件大小限制描述，如"2MB"
     */
    public String getMaxFileSizeDesc() {
        return (this.maxFileSize / 1024 / 1024) + "MB";
    }
}
